package bjtu.tjj.simpledy.controller;

import bjtu.tjj.simpledy.common.CommonResult;
import bjtu.tjj.simpledy.dto.NameAndPwdDTO;
import bjtu.tjj.simpledy.dto.PwdDTO;
import bjtu.tjj.simpledy.dto.UserDTO;
import bjtu.tjj.simpledy.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("user")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    @PostMapping("register")
    public CommonResult<Void> register(@RequestBody @Valid NameAndPwdDTO nameAndPwdDTO) {
        userService.register(nameAndPwdDTO);
        return CommonResult.success();
    }

    /**
     * 登录
     *
     * @return result: result.data表示token
     */
    @PostMapping("login")
    public CommonResult<String> login(@RequestBody @Valid NameAndPwdDTO nameAndPwdDTO) {
        return CommonResult.success(userService.login(nameAndPwdDTO));
    }

    @PostMapping("logout")
    public CommonResult<Void> logout() {
        userService.logout();
        return CommonResult.success();
    }

    @PostMapping("destroy")
    public CommonResult<Void> destroy(@RequestBody @Valid PwdDTO pwdDTO) {
        userService.destroy(pwdDTO);
        return CommonResult.success();
    }

    /**
     * @return result: result.data表示文件名
     */
    @PostMapping("avatar")
    public CommonResult<String> updateAvatar(@RequestPart("avatar") MultipartFile avatar) {
        return CommonResult.success(userService.updateAvatar(avatar));
    }

    @GetMapping
    public CommonResult<UserDTO> getSelf() {
        return CommonResult.success(userService.getSelf());
    }
}
